package quesiton;

import quesiton.SettingOp;

import javax.swing.*;
import java.awt.*;
import java.util.Random;

public class methrodConst {
    /**
     * 产生所有题目布局 根据题量题目数量动态变化
     */
    protected static int m_anseredAmount = 0;    //已答数量
    protected static int m_currentAmount = 10;    //显示题量

    public static void generateQuestion(JFrame f) {

        //总题量小于10
        if (SettingOp.m_blankAmount < m_currentAmount) {
            m_currentAmount = SettingOp.m_blankAmount;
        } else {
            m_currentAmount = SettingOp.m_blankAmount - m_anseredAmount;
            if (m_currentAmount >= 10) {
                m_currentAmount = 10;
            }
        }
        Random random = new Random();//随机数
        int x0 = 15, y0 = 5, xr = 415, anser_x = 184, anser_y = 583, judge_x = 324, judge_y = 723;
        for (int i = 1; i <= m_currentAmount; i++) {
            /************************ 问题栏  *******************************/
            JTextField question = new JTextField();
            int x1 = random.nextInt(100);  //x0为0到10之间的整数
            int y1 = random.nextInt(100);  //y0为0到10之间的整数
            int operRn = random.nextInt(4);//算法符号随机 [0,4)
            //算式产生 即答案保存
            switch (operRn) {
                case 0://-
                    question.setText(" " + String.valueOf(x1) + " - " + String.valueOf(y1) + "  = ");
                    System.out.println("当前:" + "---算式" + x1 + "-" + y1);
                    fillMath.questionStr.add(x1 + "-" + y1);
                    fillMath.anserCo.add((float) (Math.round((x1 - y1) * 100) / 100));
                    break;
                case 1://+
                    question.setText(" " + String.valueOf(x1) + " + " + String.valueOf(y1) + "  = ");
                    System.out.println("当前:" + "---算式" + x1 + "+" + y1);
                    fillMath.questionStr.add(x1 + "+" + y1);
                    fillMath.anserCo.add((float) (Math.round((x1 + y1) * 100) / 100));
                    break;
                case 2://x
                    question.setText(" " + String.valueOf(x1) + " x " + String.valueOf(y1) + "  = ");
                    System.out.println("当前:" + "---算式" + x1 + "+" + y1);
                    fillMath.questionStr.add(x1 + "*" + y1);
                    fillMath.anserCo.add((float) (Math.round(((float) x1 * (float) y1) * 100) / 100));
                    break;
                case 3://÷
                    //除法 除数为0判断
                    if (y1 == 0) {
                        y1 = random.nextInt(100);  //y0为0到10之间的整数
                    }
                    fillMath.questionStr.add(x1 + "/" + y1);
                    fillMath.anserCo.add((float) (Math.round(((float) x1 / (float) y1) * 100) / 100));
                    System.out.println("当前:" + "---算式" + x1 + "/" + y1);
                    question.setText(" " + String.valueOf(x1) + " / " + String.valueOf(y1) + "  = ");
                    break;
                default:
            }
            question.setFont(new Font("宋体", Font.PLAIN, 30));
            question.setEditable(false);
            if (i <= m_currentAmount / 2)//左边一半题
            {
                question.setBounds(x0, y0 + 70 * i, 171, 41);
            } else {//右边一半题
                question.setBounds(xr, y0 + 70 * (i - m_currentAmount / 2), 171, 41);
            }
            question.setColumns(1);
            f.add(question);
            fillMath.questionList.add(question);
            /************************ 答题栏  *******************************/
            JTextField answer = new JTextField();
            answer.setFont(new Font("宋体", Font.PLAIN, 30));
            if (i <= m_currentAmount / 2)//左边一半答案框
            {
                answer.setBounds(anser_x, y0 + 70 * i, 141, 41);
            } else {//右边一半答案框
                answer.setBounds(anser_y, y0 + 70 * (i - m_currentAmount / 2), 141, 41);
            }
            f.add(answer);
            answer.setColumns(1);
            fillMath.anserList.add(answer);
            /************************ 判断框  *******************************/
            JTextField judge = new JTextField();
            judge.setFont(new Font("宋体", Font.PLAIN, 25));
            judge.setEditable(false);
            if (i <= m_currentAmount / 2)//左边一半判断框
            {
                judge.setBounds(judge_x, y0 + 70 * i, 41, 41);
            } else {//右边一半判断框
                judge.setBounds(judge_y, y0 + 70 * (i - m_currentAmount / 2), 41, 41);
            }
            f.add(judge);
            judge.setColumns(10);
            fillMath.judgeList.add(judge);
        }
    }
}
